/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mhv.fr.sprite.animator;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import mhv.fr.utils.RessourcesManager;

/**
 * SpriteAnimator is a wrapper class use to animate sprite
 * @author Marc-Henri
 */
public class SpriteAnimator {
    private Image[] animationArray;
    private int maxCols;
    private int currentFrame;
    private int numberOfFrame;
    private int frameDelay;
    private int frameWidth;
    private int frameHeight;
    private int frameCounter;
    private int animationDirection;

    /**
     * Getter method
     * @return int
     */
    public int getAnimationDirection() {
        return animationDirection;
    }

    /**
     * Setter Method
     * @param animationDirection values '1' or '-1'
     */
    public void setAnimationDirection(int animationDirection) {
        this.animationDirection = animationDirection;
    }

    /**
     * Getter method
     * @return currentFrame
     */
    public int getCurrentFrame() {
        return currentFrame;
    }

    /**
     * Setter Method
     * @param currentFrame
     */
    public void setCurrentFrame(int currentFrame) {
        this.currentFrame = currentFrame;
    }

    /**
     * Getter method
     * @return frameDelay
     */
    public int getFrameDelay() {
        return frameDelay;
    }

    /**
     * Setter Method
     * @param frameDelay
     */
    public void setFrameDelay(int frameDelay) {
        this.frameDelay = frameDelay;
    }

    /**
     * Getter method
     * @return frameHeight
     */
    public int getFrameHeight() {
        return frameHeight;
    }

    /**
     * Setter Method
     * @param frameHeight
     */
    public void setFrameHeight(int frameHeight) {
        this.frameHeight = frameHeight;
    }

    /**
     * 
     * @return
     */
    public int getFrameWidth() {
        return frameWidth;
    }

    /**
     * Setter Method
     * @param frameWidth
     */
    public void setFrameWidth(int frameWidth) {
        this.frameWidth = frameWidth;
    }

    /**
     * Getter method
     * @return maxCols
     */
    public int getMaxCols() {
        return maxCols;
    }

    /**
     * Setter Method
     * @param maxCols
     */
    public void setMaxCols(int maxCols) {
        this.maxCols = maxCols;
    }

    /**
     * Getter method
     * @return numberOfFrame
     */
    public int getNumberOfFrame() {
        return numberOfFrame;
    }

    /**
     * Setter Method
     * @param numberOfFrame
     */
    public void setNumberOfFrame(int numberOfFrame) {
        this.numberOfFrame = numberOfFrame;
    }

    /**
     * Load this sprite animation image.
     * @param filename
     * @param columns
     * @param rows
     * @param width
     * @param height
     */
    public void load(String filename, int columns, int rows,
        int width, int height)
    {
        Image tiledImage;
        
        setMaxCols(columns);
        setNumberOfFrame(columns * rows);
        setFrameWidth(width);
        setFrameHeight(height);

        //load the tiled animation bitmap
        RessourcesManager rs = RessourcesManager.getInstance();
        tiledImage = rs.LoadImage (filename);
        tiledImage = new ImageIcon(tiledImage).getImage();
        
        //create animation array
        setAnimationImage(tiledImage, columns, rows, width, height);
    }

    /**
     * Set this sprite animation image.
     * @param filename
     * @param columns
     * @param rows
     * @param width
     * @param height
     */
    public void setAnimationImage(Image tiledImage, int columns, int rows,
        int width, int height)
    {
        BufferedImage tempImage;
        Graphics2D tempSurface;
          
        setMaxCols(columns);
        setNumberOfFrame(columns * rows);
        setFrameWidth(width);
        setFrameHeight(height);

        
        //create animation array
        animationArray = new Image[getNumberOfFrame()];
        for (int i = 0; i < animationArray.length; i++) {
            int frameX = (i % getMaxCols()) * width;
            int frameY = (i / getMaxCols()) * height;
            
            tempImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
            tempSurface = tempImage.createGraphics();
      
            tempSurface.drawImage(tiledImage, 0, 0, width-1, height-1, frameX,
                    frameY, frameX+width, frameY+height, null);
            
            tempSurface.dispose();
            animationArray[i] = tempImage;
        }

    }

    /**
     * Getter method
     * @return frameCounter
     */
    public int getFrameCounter() {
        return frameCounter;
    }

    /**
     * Setter Method
     * @param frameCounter
     */
    public void setFrameCounter(int frameCounter) {
        this.frameCounter = frameCounter;
    }
    
    
    /**
     * Update the frame animation accordint the direction and the frame delay.
     */
    public void updateAnimation() {
        frameCounter++;
        if (getFrameCounter() > getFrameDelay()) {
            setFrameCounter(0);
            
            //update the animation frame
            setCurrentFrame(getCurrentFrame() + getAnimationDirection());
            if (getCurrentFrame() > getNumberOfFrame() - 1) {
                setCurrentFrame(0);
            }
            else if (getCurrentFrame() < 0) {
                setCurrentFrame(getNumberOfFrame() - 1);
            }
        }
    }
    
    public Image getCurrentImage() {
        return animationArray[currentFrame];
    }
    
    
    /**
     * Construct and initialise the Sprite object.
     * @param g2d Graphics2D : the drawing surface
     */
    public SpriteAnimator() {

        
        animationArray=null;
        maxCols = 0;
        currentFrame = 0;
        numberOfFrame = 0;
        frameDelay = 0;
        frameWidth = 0;
        frameHeight = 0;
        frameCounter = 0;
        animationDirection = 1;
    }
   

}
